package com.suredy.app.porject.service;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.suredy.app.porject.model.ContractWorkPlan;
import com.suredy.app.porject.model.vo.WorkPlanFilterModel;
import com.suredy.core.service.BaseSrvWithEntity;
import com.suredy.core.service.OrderEntity;
import com.suredy.security.entity.UserEntity;

@Service
public class ContractWorkPlanSrv extends BaseSrvWithEntity<ContractWorkPlan> {

	public ContractWorkPlanSrv() {
		this.addAnDefOrder("datePoint", true);
	}

	@Override
	public DetachedCriteria getDc(ContractWorkPlan t) {
		DetachedCriteria dc = super.getDc(t);

		if (t == null)
			return dc;

		if (!StringUtils.isBlank(t.getId())) {
			dc.add(Restrictions.eq("id", t.getId()));
		}
		if (t.getCompleted() != null) {
			dc.add(Restrictions.eq("completed", t.getCompleted()));
		}
		if (t.getCreater() != null) {
			dc.add(Restrictions.eq("creater", t.getCreater()));
		}
		if (t.getContract() != null) {
			dc.add(Restrictions.eq("contract", t.getContract()));
		}

		return dc;
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public List<ContractWorkPlan> getAllPlanList(WorkPlanFilterModel search, int page, int pageSize) {
		if (search == null)
			search = new WorkPlanFilterModel();

		String ql = null;
		List<Object> params = null;

		List<?> ret = search.makeQl();

		if (ret == null || ret.size() != 2)
			return null;

		ql = (String) ret.get(0);
		params = (List<Object>) ret.get(1);

		List<ContractWorkPlan> data = null;

		if (params == null || params.isEmpty())
			data = (List<ContractWorkPlan>) this.readPageByQL(ql, page, pageSize);
		else
			data = (List<ContractWorkPlan>) this.readPageByQL(ql, page, pageSize, params.toArray());

		if (data == null || data.isEmpty())
			return null;

		for (ContractWorkPlan d : data) {
			d.getContract().getId();
		}

		return data;
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public int getAllPlanListCount(WorkPlanFilterModel search) {
		if (search == null)
			search = new WorkPlanFilterModel();

		String ql = null;
		List<Object> params = null;

		List<?> ret = search.makeCountQl();

		if (ret == null || ret.size() != 2)
			return 0;

		ql = (String) ret.get(0);
		params = (List<Object>) ret.get(1);

		if (params == null || params.isEmpty())
			return this.getCountByQL(ql);
		else
			return this.getCountByQL(ql, params.toArray());
	}

	@Transactional
	public List<ContractWorkPlan> getAlarmPlanList(String userId, Date lastDate) {
		if (StringUtils.isBlank(userId))
			return null;

		ContractWorkPlan search = new ContractWorkPlan();

		search.setCreater(new UserEntity(userId));
		search.setCompleted(false);

		DetachedCriteria dc = this.getDc(search);

		dc.add(Restrictions.lt("datePoint", lastDate));
		dc.addOrder(Order.asc("datePoint"));

		@SuppressWarnings("unchecked")
		List<ContractWorkPlan> data = (List<ContractWorkPlan>) this.readByCriteria(dc);

		if (data == null || data.isEmpty())
			return null;

		for (ContractWorkPlan d : data) {
			d.getContract().getId();
		}

		return data;
	}

	@Transactional
	public List<ContractWorkPlan> getAllAlarmPlanList() {
		ContractWorkPlan search = new ContractWorkPlan();

		search.setCompleted(false);

		DetachedCriteria dc = this.getDc(search);

		dc.addOrder(Order.asc("datePoint"));

		@SuppressWarnings("unchecked")
		List<ContractWorkPlan> data = (List<ContractWorkPlan>) this.readByCriteria(dc);

		if (data == null || data.isEmpty())
			return null;

		for (ContractWorkPlan d : data) {
			d.getContract().getId();
		}

		return data;
	}

	@Transactional
	public List<ContractWorkPlan> getAllPlanList(int page, int pageSize) {
		ContractWorkPlan search = new ContractWorkPlan();

		OrderEntity oe = new OrderEntity();
		oe.asc("completed");
		oe.asc("datePoint");

		List<ContractWorkPlan> data = this.readPageByEntity(search, page, pageSize, oe);

		if (data == null || data.isEmpty())
			return null;

		for (ContractWorkPlan d : data) {
			d.getContract().getId();
		}

		return data;
	}

	@Transactional
	public int getAllPlanListCount() {
		ContractWorkPlan search = new ContractWorkPlan();

		LinkedHashMap<String, Boolean> order = new LinkedHashMap<String, Boolean>();
		order.put("completed", true);
		order.put("datePoint", true);

		return this.getCountByEntity(search);
	}

	/**
	 * 获取合同的id
	 * 
	 * @param id
	 * @return
	 */
	public String getContractId(String id) {
		if (StringUtils.isBlank(id))
			return null;

		String ql = "SELECT T.contract.id FROM ContractWorkPlan T WHERE T.id=?";

		return (String) this.readSingleByQL(ql, id);
	}

}
